#! /usr/bin/env python
# -*- coding: utf-8 -*-

import os, sys, cgi, re, time, shutil, traceback, getopt, locale
from Util import *
from pygments import highlight
from pygments.lexers import PythonLexer, get_lexer_for_filename, get_lexer_for_mimetype
from pygments.formatters import HtmlFormatter
import threading, Queue
from Languages import *


def highlight_content(filepath):
    content = read_as_string(filepath)
    output = highlight_by_filename(filepath, content)
    sys.stdout.write(output)
    

#############################
# Start
#############################


usage = "beautifier.py --mimetype=text/plain --file=path"

try:
    opts, args = getopt.getopt(sys.argv[1:], ":m:f:", ["mimetype=", "file="])
except getopt.GetoptError:
    print "getopt error - exiting"
    sys.exit(2)

for opt, arg in opts:
    if opt in ("-m", "--mimetype"):
        mimetype = arg
    elif opt in ("-f", "--file"):
        path = arg
        
if vars().has_key('path'):
    try:
        highlight_content(path)
        sys.exit(0)
    except Exception,e:
        traceback.print_exc(file=sys.stdout)
        sys.exit(1)
        
else:
        print "Some options are missing."
        print "Usage:\n%s" % usage
        sys.exit(1)
